package cn.manter.system.controller;

import cn.manter.basic.utils.AjaxResult;
import cn.manter.basic.utils.PageList;
import cn.manter.system.Service.IStaffService;
import cn.manter.system.domain.Staff;
import cn.manter.system.query.StaffQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private IStaffService staffService;
    @GetMapping("/{id}")
    public Staff loadById(@PathVariable("id") Long id){
        return staffService.loadById(id);
    }

    @GetMapping
    public List<Staff> loadAll(){
        return staffService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        System.out.println("明细删除方法");
        try {
            staffService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Staff staff){
        try {
            if(staff.getId() == null){
                staffService.save(staff);
            }else {
                staffService.update(staff);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping
    public PageList<Staff> queryData(@RequestBody StaffQuery staffQuery){

        PageList<Staff> data = staffService.queryData(staffQuery);
        return data;
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            staffService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    @GetMapping("/dept/{id}")
    public List<Staff> loadByStaffId(@PathVariable("id") Long id){
        System.out.println("2222");
        return staffService.loadByStaffId(id);
    }

}
